package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.qo.TransferCreditQueryObject;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CreditTransferController {
    @Autowired
    private ICreditTransferService creditTransferService;

    /**
     * 我可以转让的债权标列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/listCanTransferCredit")
    public String listCanTransferCredit(@ModelAttribute("qo") TransferCreditQueryObject qo, Model model){
        model.addAttribute("pageInfo", creditTransferService.listCanTransferCredit(qo));
        return "/credittransfer_cantrans_list";
    }

    /**
     * 转让债权标
     * @param bidId
     * @return
     */
    @RequestMapping("/creditTransfer")
    @ResponseBody
    public JSONResult creditTransfer(Long[] bidId){
        JSONResult result = null;
        try {
            creditTransferService.creditTransfer(bidId);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

    /**
     * 转让中的债权标
     * @return
     */
    @RequestMapping("/listMyTransferCredit")
    public String listMyTransferCredit(@ModelAttribute("qo") TransferCreditQueryObject qo, Model model){
        qo.setTransFromId(UserContext.getCurrent().getId());
        qo.setState(Constants.BIDREQUEST_STATE_BIDDING);
        model.addAttribute("pageInfo", creditTransferService.queryPage(qo));
        return "credittransfer_mytrans_list";
    }

    /**
     * 认购债权转让标
     * @param id
     * @return
     */
    @RequestMapping("/subscribe")
    @ResponseBody
    public JSONResult subscribe(Long id){
        JSONResult result = null;
        try {
            creditTransferService.subscribe(id);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
